package pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.gef.commands.CommandStack;

import pl.edu.mimuw.irs.core.model.design.net.IRSNode;
import pl.edu.mimuw.irs.core.util.Converter;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.part.IRSNodeEditPart;

public abstract class AbstractNodeHandler extends AbstractCommandStackSelectionHandler {

	// *** Attributes

	// *** Methods
	
	protected abstract Object execute(ExecutionEvent event, CommandStack stack, IRSNode node) throws ExecutionException;
	
	@Override
	public Object execute(ExecutionEvent event, CommandStack stack, Object[] selection) throws ExecutionException {
		if (selection.length != 1) return null;
		if (!(selection[0] instanceof IRSNodeEditPart)) return null;
		IRSNodeEditPart nodeEditPart = (IRSNodeEditPart) selection[0];
		IRSNode node = Converter.asIRSNode(nodeEditPart.getNode());
		
		/*
		 * So we have one node in selection array, and a command stack.
		 * We can perform the command execution.
		 */
		return execute(event, stack, node);
	}

}
